﻿using System.Linq;
using System.Xml.Linq;
using NBehave.Spec.NUnit;
using NUnit.Framework;



namespace Linq2XmlExample.Tests
{
	[TestFixture]
	public class When_calling_google_api
	{
		private string url = @"http://maps.google.com/maps/geo?" +
		                     @"q=1600+Amphitheatre+Parkway,+Mountain+View,+CA&" +
		                     @"output=xml&oe=utf8\&" +
		                     @"sensor=true_or_false&" +
		                     @"key=ABQIAAAAwVMf6nPKISDEYeVf_LwauRRsECvq1bbBXuGkYzbU7D3EKDLRJhRKipMG-wZ_7q3uYv5bAocuyZbBqw";



		[Test]
		public void Should_receive_xml()
		{
			GoogleApi googleApi = new GoogleApi();
			XDocument returnedValue = googleApi.GetDocument( url );
			returnedValue.ShouldNotBeNull();
			returnedValue.Root.IsEmpty.ShouldBeFalse();
		}



		[Test]
		public void Should_receive_lattitude_and_longitude()
		{
			GoogleApi googleApi = new GoogleApi();
			XDocument doc = googleApi.GetDocument( url );

			var ns = doc.Root.GetDefaultNamespace();

			var response = doc.Root.Element( ns + "Response" );
			var placeMark = response.Element( ns + "Placemark" );
			var extendedData = placeMark.Elements( ns + "ExtendedData" );
			var latLonBox = extendedData.Elements( ns + "LatLonBox" ).Single();
			latLonBox
				.Attribute( "north" )
				.Value.ShouldEqual( "37.4251196" );
		}
	}
}